package com.zhao.system.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.system.poro.Library;
import com.zhao.system.service.LibraryService;
import com.zhao.system.vo.BookVO;
import com.zhao.system.vo.Booksvo;
import com.zhao.system.vo.pages;
import com.zhao.system.poro.Book;
import com.zhao.system.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import response.Result;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaozhao
 * @since 2020-11-08
 */
@RestController
@CrossOrigin(allowCredentials ="true")
@Transactional
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;

    //获取所有图书信息+分页
    @RequestMapping("/findpage1")
    public Result find1(@RequestParam(value = "currentPage",defaultValue = "1")Integer currentPage,
                        @RequestParam(value = "pageSize",defaultValue = "7")Integer pageSize){
        Page<Book> page=new Page<>(currentPage,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("statue",1);
        IPage<Book> userPage = bookService.getBooks(page,wrapper);
        //总记录数
        long total = userPage.getTotal();
        List<Book> records = userPage.getRecords();
        System.out.println(total);
        System.out.println(records);
        return Result.ok().data("total",total).data("recodes",records);

    }
    //根据姓名模糊查询获取所有图书信息+分页
    @RequestMapping("/getBookPage")
    public Result getBookPage(@RequestBody BookVO bookVO){
        Page<Book> page=new Page<>(bookVO.getCurrentPage(),bookVO.getPageSize());
        QueryWrapper<Book> wrapper=getWrapper(bookVO);
        IPage<Book> bookPage = bookService.getBookPage(page,wrapper);
        //总记录数
        long total = bookPage.getTotal();
        System.out.println(total);
        if (total==0){
            return Result.error();
        }
        List<Book> records = bookPage.getRecords();
        return Result.ok().data("total",total).data("recodes",records);


    }
    //构建wrapper
    public QueryWrapper<Book> getWrapper(BookVO bookVO){
        QueryWrapper<Book> wrapper=new QueryWrapper();
        if(bookVO!=null){
            if(!StringUtils.isEmpty(bookVO.getBname()))
            wrapper.like("b.name",bookVO.getBname());
        }
        return wrapper;
    }
    //添加图书
    @RequestMapping("/add")
    public Result add(@RequestBody Booksvo booksvo){
        //获取图书id
        Integer id = Integer.valueOf(booksvo.getBid());
        Integer roomid = Integer.valueOf(booksvo.getValue1());
        Integer btid = Integer.valueOf(booksvo.getValue2());
        Integer num=Integer.valueOf(booksvo.getNum());
        Book book=new Book();
        book.setBid(id);
        book.setRoomid(roomid);
        book.setAuthor(booksvo.getAuthor());
        book.setName(booksvo.getBname());
        book.setBtid(btid);
        //根据图书id查询图书是否存在
        Book book1 = bookService.findByBid(id);
        //判断图书是否为空
        if(book1==null){
            //图书为空
            boolean b = bookService.save(book);
            return Result.ok().data("message","添加成功");
        }else {
            //图书不为空，判断要添加的图书信息和查询到的图书信息是否相同
            if(book1.equals(book)){
                //如果相同就把数量加1
                bookService.updateNum(id,num);
                return Result.ok().data("message","添加成功");
            }
            else{
                //如果添加的图书和查询到的图书不完全相同就报错
                return Result.error();
            }
        }
    }
    //根据图书id获取图书
    @RequestMapping("/findbyId")
    public Result findbyid(@RequestParam("bid")Integer bid){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bid",bid);
        Map<Object,Object> book = bookService.getMap(wrapper);
        if(book==null){
            return Result.error();
        }
        return Result.ok().data("book",book);
    }
    //根据id修改图书信息
    @RequestMapping("/update")
    public Result update(@RequestBody Booksvo booksvo){
        Integer id = Integer.valueOf(booksvo.getBid());
        //把bid为id的图书的书名和作者
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("bid",id);
        Book book=new Book();
        book.setName(booksvo.getBname());
        book.setAuthor(booksvo.getAuthor());
        book.setNum(booksvo.getNum());
        boolean b = bookService.update(book,wrapper);
        if(b){
            System.out.println("1111");
        }
        return Result.ok().data("message","修改成功");
    }
    //修改图书书室名称
    @RequestMapping("/updatebyId")
    public Result updatebyid(@RequestBody Booksvo booksvo){
        if(!booksvo.getValue1().contains("-")){
            return Result.ok().data("message","修改完成");
        }
        //把roomid为booksvo.getBid()的书籍的书室号更新为rid，其他属性不变
        String[] split = booksvo.getValue1().split("-");
        Integer rid = Integer.valueOf(split[0]);
        UpdateWrapper<Book> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bid",booksvo.getBid());
        Book book=new Book();
        book.setRoomid(rid);
        boolean b = bookService.update(book, updateWrapper);
        if(b){
            return Result.ok().data("message","修改成功");
        }
        return Result.error();
    }
    //修改图书类型的名称
    @RequestMapping("/updatetype")
    public Result updatetype(@RequestBody Booksvo booksvo){
        System.out.println(booksvo.getValue2());
        if(!booksvo.getValue2().contains("-")){
            return Result.ok().data("message","修改完成");
        }
        //把bid为booksvo.getBid()的书籍号更新为btid，其他属性不变
        String[] split = booksvo.getValue2().split("-");
        Integer btid = Integer.valueOf(split[0]);
        UpdateWrapper<Book> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bid",booksvo.getBid());
        Book book=new Book();
        book.setBtid(btid);
        boolean b = bookService.update(book, updateWrapper);
        if(b){
            return Result.ok().data("message","修改成功");
        }
        return Result.error();
    }
    //进行逻辑删除
    @RequestMapping("/del")
    public Result del(Integer bid){
        int i = bookService.del(bid);
        return Result.ok();
    }

}

